
package com.tools.cleanmaster.ui.card.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.card.animator.DampingInterpolator;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.Utils;

/**
 * 布局中，带有回弹动画
 */
public class SpringRopeView extends View {

    public interface AnimationListener {
        void onAnimationCompleted();
    }

    private static final int MAX_AMPLITUDE_DEFAULT = 50;

    private static final int LINE_POSITION_DEFAULT = 0;

    private static final int AMPLITUDE_COUNT_DEFAULT = 5;

    private static final int AMPLITUDE_ONE_TIME_DEFAULT = 1000;

    private static final int BACK_COLOR_DEFAULT = 0xffffffff;

    private int mMaxAmplitude;

    private int mAmplitudeCount;

    private int mAmplitudeTime;

    private AnimationListener mListener;

    private int mBackColor;

    private int mCurrentControlY;

    private int mTotalWidth, mTotalHeight;

    private int mHalfWidth, mHalfHeight;

    private Path mPath;

    private Paint mPaint;

    private int mLinePosition;

    private int mMaxControlY;

    private boolean mIsBottom = true;

    private PaintFlagsDrawFilter mDrawFilter;

    public SpringRopeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        initData(context, attrs);
        initPath();
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Style.FILL);
        mPaint.setColor(mBackColor);
    }

    private void initPath() {
        mPath = new Path();
    }

    private void initData(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpringRopeView);
        mMaxAmplitude = typedArray.getDimensionPixelOffset(
                R.styleable.SpringRopeView_max_amplitude,
                Utils.dipToPx(context, MAX_AMPLITUDE_DEFAULT));
        mLinePosition = typedArray.getDimensionPixelOffset(
                R.styleable.SpringRopeView_line_position,
                Utils.dipToPx(context, LINE_POSITION_DEFAULT));
        mAmplitudeCount = typedArray.getInteger(R.styleable.SpringRopeView_amplitude_count,
                Utils.dipToPx(context, AMPLITUDE_COUNT_DEFAULT));
        mAmplitudeTime = typedArray.getInteger(R.styleable.SpringRopeView_amplitude_time,
                Utils.dipToPx(context, AMPLITUDE_ONE_TIME_DEFAULT));

        mMaxControlY = mLinePosition + 2 * mMaxAmplitude;
        mCurrentControlY = mMaxControlY;

        mBackColor = typedArray.getColor(R.styleable.SpringRopeView_back_color, BACK_COLOR_DEFAULT);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.setDrawFilter(mDrawFilter);
        super.onDraw(canvas);
        mPath.reset();
        mPath.moveTo(0, mLinePosition);
        mPath.lineTo(0, mTotalHeight);
        mPath.lineTo(mTotalWidth, mTotalHeight);
        mPath.lineTo(mTotalWidth, mLinePosition);
        mPath.quadTo(mHalfWidth, mCurrentControlY, 0, mLinePosition);
        canvas.drawPath(mPath, mPaint);
            LogHelper.i("SpringRopeView", mCurrentControlY - mLinePosition + "");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;

        mHalfWidth = mTotalWidth / 2;
        mHalfHeight = mTotalHeight / 2;
    }

    public void startSpring() {
        mCurrentControlY = mMaxControlY;
        ValueAnimator valueAnimator = ValueAnimator.ofInt(mCurrentControlY, mLinePosition);
        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                int value = (Integer) animator.getAnimatedValue();
                mCurrentControlY = value;
                    LogHelper.i("mCurrentControlY", "startSpring==" + mCurrentControlY);
                postInvalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mListener != null) {
                    mListener.onAnimationCompleted();
                }
            }

        });
        valueAnimator.setDuration(mAmplitudeTime);
        DampingInterpolator ip = new DampingInterpolator();
        ip.setAngularVelocity((float) (Math.PI * mAmplitudeCount));
        valueAnimator.setInterpolator(ip);
        valueAnimator.start();
    }

    public void setSpringPosition(float offsetPercent) {
        mCurrentControlY = (int) (mLinePosition + offsetPercent * (mMaxControlY - mLinePosition));
            LogHelper.i("mCurrentControlY", "setSpringPosition==" + mCurrentControlY);
        this.postInvalidate();
    }

    public void addAnimationListener(AnimationListener listener) {
        mListener = listener;
    }

    public long getAmplitudeTime() {
        return mAmplitudeTime;
    }
}
